package com.atguigu.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: KeywordUtil
 * Package: com.atguigu.utils
 * Description:
 *
 * @Author Lovxy
 * @Create 2023/5/15 10:47
 * @Version 1.0
 */
public class KeywordUtil {
    public static List<String> splitKeyword(String keyword) throws IOException {

        //创建集合
        ArrayList<String> list = new ArrayList<>();

        //创建IK分词器对象
        IKSegmenter ikSegmenter = new IKSegmenter(new StringReader(keyword), false);

        //遍历切分出的词
        Lexeme next = ikSegmenter.next();
        while (next != null) {
            String word = next.getLexemeText();
            list.add(word);
            next = ikSegmenter.next();
        }

        //返回结果
        return list;
    }

}
